package com.android.topview.watermelon.util

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import kotlin.math.ceil

private fun Context.getStatusBarHeight(): Int {
    val statusBarHeight = ceil(25 * resources.displayMetrics.density)
    return statusBarHeight.toInt()
}

// 开启沉浸式状态栏和导航栏
fun Activity.clearSystemWindowsDecorFits(
    statusBar: TextView? = null,
    color: Int = Color.TRANSPARENT
) {
    WindowCompat.setDecorFitsSystemWindows(window, false)
    statusBar?.height = getStatusBarHeight()
    window.statusBarColor = color
    window.navigationBarColor = Color.TRANSPARENT
}

// 设置状态栏文本颜色
fun Activity.setStatusBarTextColor(isWhite: Boolean) {
    ViewCompat.getWindowInsetsController(window.decorView)
        ?.isAppearanceLightStatusBars = !isWhite
}

fun Context.isLightTheme() =
    applicationContext.resources.configuration.uiMode == 0x11
